/**
 * Created by BenYip-zt on 4/21/2017.
 */

var augend = document.getElementById('augend'),
    addend = document.getElementById('addend'),
    sum = document.getElementById('sum');

sum.disabled = true;

var checkbox = document.getElementById('instant');
var isInstant = checkbox.checked;
checkbox.addEventListener('change', function () {
    isInstant = checkbox.checked;
});

function getSum() {
    var a = parseFloat(augend.value),
        b = parseFloat(addend.value);

    if (isNaN(a) || isNaN(b)) return false;

    return (a + b).toFixed(2);
}

function instantListener() {
    var _sum = getSum();
    if (_sum && isInstant) sum.value = _sum;
}

augend.addEventListener('input', instantListener);
addend.addEventListener('input', instantListener);

document.addEventListener('keydown', function (e) {
    if (e.keyCode === 13) {
        sum.value = getSum();
    }
});